<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="&amp;#x524D;&amp;#x8A00;&amp;#x4E4B;&amp;#x524D;&amp;#x4E00;&amp;#x76F4;&amp;#x5BF9;socket&amp;#x7F16;&amp;#x7A0B;&amp;#x8FD9;&amp;#x5757;&amp;#x6BD4;&amp;#x8F83;&amp;#x964C;&amp;#x751F;&amp;#xFF0C;&amp;#x5E76;&amp;#x4E14;&amp;#x5728;&amp;#x521A;&amp;#x5F00;&amp;#x59CB;&amp;#x5DE5;&amp;#x4F5C;&amp;#">
<meta property="og:type" content="article">
<meta property="og:title" content="Android Socket编程（tcp）初探">
<meta property="og:url" content="http://yoursite.com/2019/08/21/Android-Socket编程（tcp）初探/index.html">
<meta property="og:site_name" content="Silence潇湘夜雨">
<meta property="og:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x4E4B;&amp;#x524D;&amp;#x4E00;&amp;#x76F4;&amp;#x5BF9;socket&amp;#x7F16;&amp;#x7A0B;&amp;#x8FD9;&amp;#x5757;&amp;#x6BD4;&amp;#x8F83;&amp;#x964C;&amp;#x751F;&amp;#xFF0C;&amp;#x5E76;&amp;#x4E14;&amp;#x5728;&amp;#x521A;&amp;#x5F00;&amp;#x59CB;&amp;#x5DE5;&amp;#x4F5C;&amp;#">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-ff87e4994d2b6574.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-3fa7b4355b1c03e6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-a0dfc130f99a50e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1716569-603d01c240080afa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-07-19T04:31:13.360Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android Socket编程（tcp）初探">
<meta name="twitter:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x4E4B;&amp;#x524D;&amp;#x4E00;&amp;#x76F4;&amp;#x5BF9;socket&amp;#x7F16;&amp;#x7A0B;&amp;#x8FD9;&amp;#x5757;&amp;#x6BD4;&amp;#x8F83;&amp;#x964C;&amp;#x751F;&amp;#xFF0C;&amp;#x5E76;&amp;#x4E14;&amp;#x5728;&amp;#x521A;&amp;#x5F00;&amp;#x59CB;&amp;#x5DE5;&amp;#x4F5C;&amp;#">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/1716569-ff87e4994d2b6574.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/08/21/Android-Socket编程（tcp）初探/">





  <title>Android Socket编程（tcp）初探 | Silence潇湘夜雨</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Silence潇湘夜雨</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Android开发一枚</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home //首页"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user //关于"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags  //标签"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th //分类"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive //归档"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar //日程表"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap //站点地图"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/21/Android-Socket编程（tcp）初探/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Silence潇湘夜雨">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Silence潇湘夜雨">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android Socket编程（tcp）初探</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-21T15:51:43+08:00">
                2019-08-21
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/socket/" itemprop="url" rel="index">
                    <span itemprop="name">socket</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="&#x524D;&#x8A00;"><a href="#&#x524D;&#x8A00;" class="headerlink" title="&#x524D;&#x8A00;"></a>&#x524D;&#x8A00;</h3><p>&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x5BF9;socket&#x7F16;&#x7A0B;&#x8FD9;&#x5757;&#x6BD4;&#x8F83;&#x964C;&#x751F;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x521A;&#x5F00;&#x59CB;&#x5DE5;&#x4F5C;&#x7684;&#x65F6;&#x5019;&#x6BD4;&#x8F83;&#x6297;&#x62D2;&#x3002;&#x5176;&#x5B9E;&#xFF0C;&#x90FD;&#x662F;&#x56E0;&#x4E3A;&#x5F53;&#x65F6;&#x81EA;&#x5DF1;&#x6BD4;&#x8F83;&#x83DC;&#xFF0C;&#x8FD9;&#x5757;&#x6BD4;&#x8F83;&#x96BE;&#x5904;&#x7406;&#xFF0C;&#x5728;&#x8212;&#x9002;&#x533A;&#x5446;&#x7684;&#x4E60;&#x60EF;&#x4E86;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x8FD8;&#x662F;&#x5E94;&#x8BE5;&#x8BA9;&#x81EA;&#x5DF1;&#x8D70;&#x51FA;&#x8212;&#x9002;&#x533A;&#xFF0C;&#x591A;&#x63A5;&#x89E6;&#x4E00;&#x4E9B;&#x964C;&#x751F;&#x7684;&#x533A;&#x57DF;&#x3002;</p>
<h3 id="&#x9996;&#x5148;"><a href="#&#x9996;&#x5148;" class="headerlink" title="&#x9996;&#x5148;"></a>&#x9996;&#x5148;</h3><p> &#x5728;&#x5C06;socket&#x7F16;&#x7A0B;&#x524D;&#xFF0C;&#x5148;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;socket&#x7684;&#x77E5;&#x8BC6;&#x3002;</p>
<h4 id="TCP-IP&#x534F;&#x8BAE;"><a href="#TCP-IP&#x534F;&#x8BAE;" class="headerlink" title="TCP/IP&#x534F;&#x8BAE;"></a>TCP/IP&#x534F;&#x8BAE;</h4><p>&#x6211;&#x4EEC;&#x4E3E;&#x4E2A;&#x4E0D;&#x6070;&#x5F53;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;&#x6BD4;&#x5982;&#x901A;&#x8FC7;QQ&#x548C;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x901A;&#x4FE1;&#xFF0C;&#x90FD;&#x9700;&#x8981;&#x54EA;&#x4E9B;&#x4E1C;&#x897F;&#x5462;&#xFF1F;<br>&#x4E24;&#x53F0;&#x7535;&#x8111;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x8FDB;&#x884C;&#x901A;&#x4FE1;&#xFF0C;&#x9700;&#x8981;&#x77E5;&#x9053;&#x53CC;&#x65B9;&#x7684;&#x5730;&#x5740;&#xFF08;&#x4E5F;&#x5C31;&#x662F;IP&#x5730;&#x5740;&#xFF09;&#xFF1B;&#x77E5;&#x9053;&#x4E24;&#x53F0;&#x7535;&#x8111;&#x7684;IP&#x5730;&#x5740;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x77E5;&#x9053;&#x6211;&#x53D1;&#x9001;&#x5230;&#x76EE;&#x6807;&#x7535;&#x8111;&#x7684;&#x76EE;&#x6807;&#x8F6F;&#x4EF6;&#xFF08;&#x4F7F;&#x7528;&#x7AEF;&#x53E3;&#x6807;&#x8BB0;&#xFF09;&#x3002;&#x5982;&#x679C;&#x4E24;&#x53F0;&#x7535;&#x8111;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x4E4B;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x901A;&#x4FE1;&#x4E86;&#x3002;<br>&#x90A3;&#x4E48;&#x8FD9;&#x4E9B;&#x4E1C;&#x897F;&#x5982;&#x4F55;&#x8FDB;&#x884C;&#x89C4;&#x5B9A;&#x7684;&#x5462;&#xFF0C;&#x8FD9;&#x5C31;&#x9700;&#x8981;&#x6709;&#x4E00;&#x5B9A;&#x7684;&#x901A;&#x8BAF;&#x534F;&#x8BAE;&#xFF0C;&#x6BD4;&#x5982;&#x6211;&#x548C;&#x5F20;&#x4E09;&#x7EA6;&#x5B9A;&#x5728;&#x897F;&#x5B89;&#x949F;&#x697C;&#x89C1;&#x9762;&#xFF0C;&#x7136;&#x540E;&#x4E24;&#x4E2A;&#x4EBA;&#x90FD;&#x5FC5;&#x987B;&#x624B;&#x62FF;&#x4E00;&#x628A;&#x767D;&#x8272;&#x8309;&#x8389;&#x82B1;&#x3002;&#x53EA;&#x6709;&#x5F53;&#x6211;&#x4EEC;&#x53CC;&#x65B9;&#x89C1;&#x9762;&#x5E76;&#x4E14;&#x770B;&#x5230;&#x5BF9;&#x65B9;&#x62FF;&#x7684;&#x662F;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x5546;&#x91CF;&#x597D;&#x7684;&#x767D;&#x8272;&#x8309;&#x8389;&#x82B1;&#x624D;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x901A;&#x8BAF;&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x8FD9;&#x4E2A;&#x767D;&#x8272;&#x8309;&#x8389;&#x82B1;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x4E4B;&#x95F4;&#x7684;&#x7EA6;&#x5B9A;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;socket&#x4E2D;&#x7684;&#x534F;&#x8BAE;&#x3002;&#x5927;&#x5BB6;&#x90FD;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#xFF0C;&#x7EDF;&#x4E00;&#x6210;&#x4E00;&#x4E2A;&#x89C4;&#x8303;&#xFF0C;&#x8FD9;&#x6837;&#x7B26;&#x5408;&#x8FD9;&#x4E2A;&#x89C4;&#x8303;&#x7684;&#x5404;&#x79CD;&#x8BBE;&#x5907;&#x4E4B;&#x95F4;&#x80FD;&#x591F;&#x8FDB;&#x884C;&#x517C;&#x5BB9;&#x6027;&#x7684;&#x901A;&#x4FE1;&#x3002;<br>&#x6700;&#x4E3A;&#x5E7F;&#x6CDB;&#x7684;&#x7684;&#x534F;&#x8BAE;&#x5C31;&#x662F;OSI&#x534F;&#x8BAE;&#x548C;TCP/IP&#x534F;&#x8BAE;&#x4E86;&#xFF0C;&#x4F46;&#x662F;OSI&#x534F;&#x8BAE;&#x8F83;&#x4E3A;&#x7E41;&#x7410;&#xFF0C;&#x672A;&#x63A8;&#x5E7F;&#xFF08;&#x60F3;&#x4E86;&#x89E3;&#x7684;&#x81EA;&#x5DF1;Google&#xFF09;&#x3002;&#x53CD;&#x800C;TCP/IP(transfer control protocol/internet protocol,&#x4F20;&#x8F93;&#x63A7;&#x5236;&#x534F;&#x8BAE;/&#x7F51;&#x9645;&#x534F;&#x8BAE;)&#x534F;&#x8BAE;&#x7B80;&#x5355;&#x660E;&#x4E86;&#xFF0C;&#x5F97;&#x5230;&#x73B0;&#x4ECA;&#x7684;&#x5E7F;&#x6CDB;&#x4F7F;&#x7528;&#x3002;<br><img src="https://upload-images.jianshu.io/upload_images/1716569-ff87e4994d2b6574.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x534F;&#x8BAE;.png"></p>
<p><img src="https://upload-images.jianshu.io/upload_images/1716569-3fa7b4355b1c03e6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x534F;&#x8BAE;&#x4F20;&#x8F93;&#x793A;&#x610F;&#x56FE;.jpg"></p>
<h3 id="&#x5176;&#x6B21;"><a href="#&#x5176;&#x6B21;" class="headerlink" title="&#x5176;&#x6B21;"></a>&#x5176;&#x6B21;</h3><p> &#x8BF4;&#x4E86;&#x90A3;&#x4E48;&#x591A;&#x7684;&#x6587;&#x5B57;&#x6027;&#x63CF;&#x8FF0;&#xFF0C;&#x90A3;&#x4E48;&#x63A5;&#x4E0B;&#x6765;&#x770B;&#x770B;&#x6211;&#x4EEC;&#x7684;&#x57FA;&#x4E8E;TCP&#x534F;&#x8BAE;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x670D;&#x52A1;&#x7AEF;&#x5B9E;&#x73B0;</p>
<ul>
<li><p>&#x5BA2;&#x6237;&#x7AEF;&#x56FE;&#x7247;<br> &#x8FD9;&#x91CC;&#x5C01;&#x88C5;&#x5230;&#x4E86;&#x7EBF;&#x7A0B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x4FEE;&#x6539;&#xFF0C;&#x90A3;&#x4E48;&#x81EA;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x5C0F;&#x53EF;&#x8FD9;&#x91CC;&#x53EA;&#x662F;&#x629B;&#x7816;&#x5F15;&#x7389;&#xFF0C;&#x5E9F;&#x8BDD;&#x4E0D;&#x8BF4;&#x5148;&#x4E0A;&#x56FE;<br> <img src="https://upload-images.jianshu.io/upload_images/1716569-a0dfc130f99a50e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="tcp&#x5BA2;&#x6237;&#x7AEF;.png"></p>
</li>
<li><p>&#x5BA2;&#x6237;&#x7AEF;&#x4EE3;&#x7801;&#xFF08;&#x7EBF;&#x7A0B;&#x4E2D;&#xFF09;<br><strong>&#x4EE3;&#x7801;&#x4E2D;&#x7684;&#x6CE8;&#x91CA;&#x6BD4;&#x8F83;&#x8BE6;&#x7EC6;&#xFF0C;&#x6211;&#x8FD9;&#x91CC;&#x5C31;&#x4E0D;&#x9010;&#x4E00;&#x89E3;&#x91CA;&#x4E86;</strong></p>
<pre><code>public class TcpClientThread extends Thread {

//IP&#x5730;&#x5740;
private String address;
//&#x7AEF;&#x53E3;
private int port;
//&#x53D1;&#x9001;&#x5185;&#x5BB9;
private String msg;
private Handler mHandler;

public TcpClientThread(Handler handler, String address, int port, String msg) {
  this.mHandler = handler;
  this.address = address;
  this.port = port;
  this.msg = msg;
}

@Override
public void run() {
  super.run();
  sendSocket();
}

/**
 * &#x8BBE;&#x7F6E;
 */
private void sendSocket() {
  InputStreamReader reader = null;
  BufferedReader bufReader = null;
  Socket socket = null;
  try {
      //1.&#x521B;&#x5EFA;&#x76D1;&#x542C;&#x6307;&#x5B9A;&#x670D;&#x52A1;&#x5668;&#x5730;&#x5740;&#x4EE5;&#x53CA;&#x6307;&#x5B9A;&#x670D;&#x52A1;&#x5668;&#x76D1;&#x542C;&#x7684;&#x7AEF;&#x53E3;&#x53F7;
      //IP&#x5730;&#x5740;&#xFF0C;&#x7AEF;&#x53E3;&#x53F7;
      socket = new Socket(address, port);
      // 2.&#x62FF;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x7684;socket&#x5BF9;&#x8C61;&#x7684;&#x8F93;&#x51FA;&#x6D41;&#x53D1;&#x9001;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x6570;&#x636E;
      OutputStream os = socket.getOutputStream();
      //&#x5199;&#x5165;&#x8981;&#x53D1;&#x9001;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x7684;&#x6570;&#x636E;
      os.write(msg.getBytes());
      os.flush();
      socket.shutdownOutput();
      //&#x62FF;&#x5230;socket&#x7684;&#x8F93;&#x5165;&#x6D41;&#xFF0C;&#x8FD9;&#x91CC;&#x5B58;&#x50A8;&#x7684;&#x662F;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;
      InputStream is = socket.getInputStream();
      //&#x89E3;&#x6790;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;
      reader = new InputStreamReader(is);
      bufReader = new BufferedReader(reader);
      String s = null;
      final StringBuffer sb = new StringBuffer();
      while ((s = bufReader.readLine()) != null) {
          sb.append(s);
      }
      sendMsg(0, sb.toString());
  } catch (UnknownHostException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  } finally { //3&#x3001;&#x5173;&#x95ED;IO&#x8D44;&#x6E90;
      try {
          if (bufReader != null)
              bufReader.close();
      } catch (IOException ex) {
          ex.printStackTrace();
      }
      try {
          if (socket != null)
              socket.close();
      } catch (IOException ex) {
          ex.printStackTrace();
      }

  }
 }

/**
 * &#x53D1;&#x9001;&#x6D88;&#x606F;
 */
private void sendMsg(int what, Object object) {
  Message msg = new Message();
  msg.what = what;
  msg.obj = object;
  mHandler.sendMessage(msg);
}
}</code></pre></li>
<li><p>&#x5BA2;&#x6237;&#x7AEF;&#xFF08;Activity&#x4E2D;&#x4F7F;&#x7528;&#xFF09;</p>
<pre><code>public class TcpClientActivity extends AppCompatActivity 
   implements   View.OnClickListener {

  EditText mEdtContent;
 TextView mTxtContent;
 Button mBtnSend;
 String address = &quot;192.168.0.197&quot;;
 int port = 12345;</code></pre></li>
</ul>
<pre><code> @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_tcpclient);
  initView();
 }

private void initView() {
  mEdtContent = findViewById(R.id.edt_content);
  mBtnSend = findViewById(R.id.btn_send);
  mTxtContent = findViewById(R.id.txt_content);
  mBtnSend.setOnClickListener(this);
}

 @Override
 public void onClick(View view) {
  switch (view.getId()) {
      case R.id.btn_send: {//&#x53D1;&#x9001;
          TcpClientThread mThread = new TcpClientThread(mHandler, address, port,
                  mEdtContent.getText().toString());
          mThread.start();

          break;
      }
  }
}

/**
 * Handler
 */
private Handler mHandler = new Handler(Looper.myLooper()) {
  @Override
  public void handleMessage(Message msg) {
      super.handleMessage(msg);
      switch (msg.what) {
          case 0: {
              String content= (String) msg.obj;
              mTxtContent.setText(content);
              break;
          }
      }
  }
};
}</code></pre><ul>
<li><p>&#x670D;&#x52A1;&#x7AEF;&#xFF08;&#x56FE;&#x7247;&#xFF09;</p>
<p> <img src="https://upload-images.jianshu.io/upload_images/1716569-603d01c240080afa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="tcp&#x670D;&#x52A1;&#x7AEF;.png"></p>
</li>
<li><p>&#x670D;&#x52A1;&#x7AEF;&#xFF08;&#x4EE3;&#x7801;&#x2013;&gt;&#x7EBF;&#x7A0B;&#x4E2D;&#xFF09;</p>
<pre><code>public class TcpServerThread extends Thread {</code></pre><p>   private Socket socket;</p>
<p>  /**</p>
<ul>
<li><p>&#x521D;&#x59CB;&#x5316;</p>
</li>
<li></li>
<li><p>@param socket<br>   */</p>
<pre><code>public TcpServerThread(Socket socket) {</code></pre><p>   this.socket = socket;</p>
<pre><code>}</code></pre><p>@Override<br>public void run() {<br>&#x200B;    super.run();</p>
<p>InputStreamReader reader = null;<br>BufferedReader bufReader = null;<br>OutputStream os = null;<br>try {<br>   reader = new InputStreamReader(socket.getInputStream());<br>   bufReader = new BufferedReader(reader);<br>   String s = null;<br>   StringBuffer sb = new StringBuffer();<br>   while ((s = bufReader.readLine()) != null) {</p>
<pre><code>sb.append(s);</code></pre><p>   }<br>   System.out.println(&#x201C;&#x670D;&#x52A1;&#x5668;&#xFF1A;&#x201D; + sb.toString());<br>   // &#x5173;&#x95ED;&#x8F93;&#x5165;&#x6D41;<br>   socket.shutdownInput();</p>
<p>   // &#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x6570;&#x636E;<br>   os = socket.getOutputStream();<br>   os.write((&#x201C;&#x6211;&#x662F;&#x670D;&#x52A1;&#x7AEF;,&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x7ED9;&#x6211;&#x7684;&#x6570;&#x636E;&#x5C31;&#x662F;&#xFF1A;&#x201D; + sb.toString()).getBytes());<br>   os.flush();<br>   socket.shutdownOutput();<br>} catch (IOException e2) {<br>   e2.printStackTrace();<br>} finally {// &#x5173;&#x95ED;IO&#x8D44;&#x6E90;<br>   if (reader != null) {</p>
<pre><code>try {
    reader.close();
} catch (IOException e) {
    e.printStackTrace();
}</code></pre><p>   }</p>
<p>   if (bufReader != null) {</p>
<pre><code>try {
    bufReader.close();
} catch (IOException e) {
    e.printStackTrace();
}</code></pre><p>   }<br>   if (os != null) {</p>
<pre><code>try {
    os.close();
} catch (IOException e) {
    e.printStackTrace();
}</code></pre><p>   }<br> }</p>
<p>}</p>
<p>}</p>
</li>
</ul>
</li>
<li><p>&#x670D;&#x52A1;&#x7AEF;&#xFF08;&#x8C03;&#x7528;&#xFF09;</p>
<pre><code>public class TcpServer {

   /**
    * @param args
    */
      public static void main(String[] args) {
      try {
    &#x200B;     @SuppressWarnings(&quot;resource&quot;)
    &#x200B;    ServerSocket serverSocket = new ServerSocket(12345);
    &#x200B;    while (true) {
    &#x200B;        System.out.println(&quot;Server&#x5F00;&#x59CB;~~~&#x76D1;&#x542C;~~~&quot;);
    &#x200B;        // accept&#x65B9;&#x6CD5;&#x4F1A;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#x4E0E;&#x4E4B;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;
    &#x200B;        Socket socket = serverSocket.accept();
    &#x200B;        TcpServerThread serverThread = new TcpServerThread(socket);
    &#x200B;        serverThread.start();
    &#x200B;    }
    } catch (IOException e) {
    &#x200B;    e.printStackTrace();
      }
      }

  }</code></pre></li>
<li><p>&#x8BF4;&#x660E;<br>&#x8FD9;&#x91CC;&#x7528;&#x4E86;&#x4E00;&#x4E2A;while&#x5FAA;&#x73AF;&#xFF0C;&#x7136;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x65E0;&#x9650;&#x63A5;&#x6536;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x679C;&#x628A;while&#x4E2D;&#x7684;&#x6761;&#x4EF6;&#x6539;&#x6210;&#x4F60;&#x9700;&#x8981;&#x7684;&#xFF0C;&#x5C31;&#x4F1A;&#x5B9E;&#x73B0;&#x4F60;&#x6240;&#x9700;&#x8981;&#x7684;&#x4E1C;&#x897F;&#x3002;</p>
<h3 id="&#x6700;&#x540E;"><a href="#&#x6700;&#x540E;" class="headerlink" title="&#x6700;&#x540E;"></a>&#x6700;&#x540E;</h3><p> &#x521A;&#x5F00;&#x59CB;&#x5199;&#x7684;&#x65F6;&#x5019;&#x81EA;&#x5DF1;&#x4E5F;&#x662F;&#x4E00;&#x8138;&#x61F5;&#x903C;&#xFF0C;&#x514B;&#x670D;&#x4E86;&#x91CD;&#x91CD;&#x56F0;&#x96BE;&#x624D;&#x505A;&#x4E86;&#x51FA;&#x6765;&#xFF0C;&#x5F53;&#x7136;&#x8FD9;&#x79CD;&#x4E5F;&#x4E0D;&#x662F;&#x6700;&#x7EC8;&#x7684;&#xFF0C;&#x6CA1;&#x6709;&#x52A0;&#x5165;&#x81EA;&#x5DF1;&#x7684;&#x534F;&#x8BAE;&#x3002;&#x9700;&#x8981;&#x6839;&#x636E;&#x81EA;&#x5DF1;&#x7684;&#x5B9E;&#x9645;&#x9700;&#x6C42;&#x505A;&#x51FA;&#x6765;&#xFF0C;&#x7136;&#x540E;&#x6539;&#x6210;&#x81EA;&#x5DF1;&#x7684;&#x9700;&#x8981;&#x7684;&#x4E1C;&#x897F;&#x3002;</p>
<h3 id="&#x81F4;&#x8C22;"><a href="#&#x81F4;&#x8C22;" class="headerlink" title="&#x81F4;&#x8C22;"></a>&#x81F4;&#x8C22;</h3><p><a href="https://www.jianshu.com/p/fb4dfab4eec1" target="_blank" rel="noopener">Android&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;&#x4E4B;&#x2013;Socket&#x7F16;&#x7A0B;</a></p>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/21/Android-DatePicker/" rel="next" title="Android DatePicker">
                <i class="fa fa-chevron-left"></i> Android DatePicker
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/21/Android-Socket编程（udp）初探/" rel="prev" title="Android Socket编程（udp）初探">
                Android Socket编程（udp）初探 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Silence潇湘夜雨</p>
              <p class="site-description motion-element" itemprop="description">Android开发一枚</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/muyishuangfeng" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/1e2eec6f972c" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/58df0abf61ff4b006b115e43" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-star"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yangke20792452" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#首先"><span class="nav-number">2.</span> <span class="nav-text">首先</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#TCP-IP协议"><span class="nav-number">2.1.</span> <span class="nav-text">TCP/IP协议</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#其次"><span class="nav-number">3.</span> <span class="nav-text">其次</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#最后"><span class="nav-number">4.</span> <span class="nav-text">最后</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#致谢"><span class="nav-number">5.</span> <span class="nav-text">致谢</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Silence潇湘夜雨</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">114.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  <!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618},"display":{"superSample":2,"width":150,"height":300,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":false},"react":{"opacityDefault":0.5,"opacityOnHover":0.5},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
